$(function() {

	/*	if(name != '' && name != null) {
			if(userType != '' && userType != null && userType == 1) {
				$('#shopManager').css("display", "none");
			}

			$('#register1').css("display", "none");
			$('#login1').css("display", "none");
			$('#userName1').css("display", "block");
			$('#logout1').css("display", "block");
			$("#userName").text(name);

			getUserInfoByUserId();
		}*/
	getUserInfoByUserId();
});

//退出登录
function logout() {
	/*if(!layer.confirm("确定退出吗？")) {
		return;
	}*/
	//删除回话

	sessionStorage.removeItem("userId");
	$.ajax({
		url: urlcore + "/api/login/logout",
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {

				window.location.href = "1.0.0denglu.html";
			} else {
				layer.alert(data.msg);
			}
		},
		error: function() {
			/* Act on the event */
			layer.alert("error");
		}
	});
}
$(function() {
	//登陆按钮绑定回车键
	$(document).keydown(function(event) {
		if(event.keyCode == 13) {
			getLogin();
		}
	});
})

function getLogin() {
	var phone = $('#phone').val().trim();
	if(phone == '') {
		layer.alert("请输入手机号码！");
		return false;
	}
	var password = $('#password').val().trim();
	if(password == '') {
		layer.alert("请输入登录密码！");
		return false;
	}
	$.ajax({
		url: urlcore + "/api/user/loginByPhone",
		type: "post",
		contentType: "application/json;charset=utf-8",
		data: JSON.stringify({
			"phone": phone,
			"password": password
		}),
		dataType: "json",
		success: function(data) {
			if(data.success == true) {
				if(layer.confirm('登录成功')) {
					//layer.confirm('登录成功')
					var da = data.data;
					//var url="index.html?userName="+da.userName;
					//window.location.href = encodeURI(url);

					if(window.sessionStorage) {
						//登录名
						//sessionStorage.setItem("userName", da.userName);
						//登录类型
						///sessionStorage.setItem("userType", da.userType);
						//sessionStorage.setItem("userId", da.id);
						//ryToken
						//sessionStorage.setItem("ryToken", da.ryToken);
						//设置cookie
						var domain_url = urlcore.split('://')[1];
						if(domain_url.indexOf(':') > -1) {
							domain_url = domain_url.split(':')[0];
						} else {
							domain_url = domain_url.split('/')[0];
						}
						document.cookie = "JSESSIONID=" + data.data.token + ";path=/;domain=" + domain_url;

						//	sessionStorage.setItem("userId",da.id);
						window.location.href = "index.html";
					} else {
						layer.alert("浏览暂不支持sessionStorage");
					}

					//或者 if(typeof window.localStorage == 'undefined'){ 	alert("浏览暂不支持localStorage") }
					//刷新
					//location.reload();

				} else {
					//跳转列表页
					window.location.href = "1.0.0denglu.html";
				}
			} else if(data.code == 'OVERTIME') {
				var thisUrl = window.location.href;
				if(thisUrl.indexOf('1.0.0denglu.html') <= -1) {
					top.window.location.href = "1.0.0denglu.html";
				}
			} else {
				layer.alert(data.msg);
			}
		},
		error: function() {
			layer.alert("error");
		}

	});
}

function getUserInfoByUserId() {
	$("#userInfo").html("");
	$.ajax({
		url: urlcore + "/api/user/getUserInfo",
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				var da = data.data;
				sessionStorage.setItem("userId", da.id);
				sessionStorage.setItem("userName", da.userName);
				//ryToken
				sessionStorage.setItem("ryToken", da.ryToken);
				if(da.userName != '' && da.userName != null) {
					if(da.userType != '' && da.userType != null && da.userType == 1) {
						$('#shopManager').css("display", "none");
					}

					$('#register1').css("display", "none");
					$('#login1').css("display", "none");
					$('#userName1').css("display", "block");
					$('#logout1').css("display", "block");
					$("#userName").text(da.userName);

				}

				var thislist =
					'<dl class="fl clearfix">' +
					'<dt>' +
					'<img src="' + da.headImg + '">' +
					'</dt>' +
					'<dd class="putong_geren_dd">' +
					'<h6>' + da.userName + '</h6>' +
					'<p>会员等级：<span>LV' + da.bullLevel + '</span></p>' +
					'<p>注册时间：' + new Date(da.gmtDatetime).pattern("yyyy-MM-dd") + '</p>' +
					'</dd>' +
					'</dl>';
				$('#userInfo').append(thislist);
			}
		},
		error: function() {
			layer.alert("error");
		}
	});
}